package IA;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author jmille80
 */
public class Service implements ClockObserverInterface,ServerWatcherInterface {

    private int TableId;
    private int ServerId;
    private int NumCustomers;
    private boolean FoodDelivered = false;
    private int RemainingEatingTime;
    private int TimeWaitingForFood = 0;
    private ArrayList<OrderItem> Items;
    private ArrayList<ServiceObserverInterface> ServiceObservers;

    public Service(int numcustomers, int tableid, int serverid, int EatingTime) {
        TableId = tableid;
        ServerId = serverid;
        NumCustomers = numcustomers;
        ServiceObservers = new ArrayList<ServiceObserverInterface>();
        Items = new ArrayList<OrderItem>();
        RemainingEatingTime = EatingTime;
        SystemClock.getInstance().AddObserver(this);
    }

    
    @Override
    public void clockTick() {
        if (FoodDelivered) {
            if (RemainingEatingTime <= 0) {
                //remove itself from clock observer list
                SystemClock.getInstance().RemoveObserver(this);
                this.NotifyServiceComplete();
            } else {
                RemainingEatingTime--;
            }
        }
        else TimeWaitingForFood++;
    }

    public void AddObserver(ServiceObserverInterface ServiceOb) {
        ServiceObservers.add(ServiceOb);
    }

    public void RemoveObserver(ServiceObserverInterface ServiceOb) {
        ServiceObservers.remove(ServiceOb);
    }

    private void NotifyServiceComplete() {
        SystemClock.getInstance().RemoveObserver(this);
        for (ServiceObserverInterface s : ServiceObservers) {
            s.notifyServiceComplete(this);
        }
    }

    public void orderItem(String name, double price, int prepTime) {
        Items.add(new OrderItem(name, price, prepTime, TableId));
    }

    public void removeOrderItem(int ItemIndex) {
        Items.remove(ItemIndex);
    }

    public OrderItem getItem(int Index) {
        return Items.get(Index);
    }

    public Iterator<OrderItem> getItemIterator() {
        return Items.iterator();
    }

    public int getNumCustomers() {
        return NumCustomers;
    }

    public int getTableID() {
        return TableId;
    }

    public int getServerID() {
        return ServerId;
    }

    public int getNumItems() {
        return Items.size();
    }
    
    public int getFoodWaitingTime() {return TimeWaitingForFood;}

    @Override
    public void OrderDelivered() {
        FoodDelivered = true;
    }




}
